package com.tubi.polymorphic2;

public class test3Polymorphic {
    /*

    好处 提高了程序的扩展性
    弊端 不能使用 子类的特有功能
     */
    public static void main(String[] args) {
        useAnimal(new Dog());
        useAnimal(new Cat());
    }
    public static  void useAnimal(Animal a ){
        a.eat();

        //instanceof 判断左边变量名字是否等于右边的类型
        if( a instanceof  Dog){
            Dog dog = (Dog)a;
            dog.method();
        }
    }
}

abstract class Animal {
    public abstract void eat();
}
class Dog extends Animal{
    @Override
    public void eat() {
        System.out.println("狗吃肉");
    }
    public void method(){
        System.out.println("看家");
    }
}
class Cat extends Animal{
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}
